# -*- coding: utf-8 -*-
from mutagen import File

from Exceptions.TagReaderException import TagReaderException

"""
Liest die Meta-Daten aus Ogg-Dateien ein. Wird im Normalfall über die Wrapper-
Klasse TagReader.Reader aufgerufen.

@author: Philipp Dermitzel
@date: 2011-03-31
@version: 1.3.0
"""
class OggReader(object):
    
    """
    ----------------------------------------------------------------------------
    Konstruktor
    ----------------------------------------------------------------------------
    """
    
    """
    Der Konstruktor. Wirft TagReaderExceptions!
    @param path: Der Pfad zur (Audio-)Datei.
    @type path: String
    """
    def __init__(self, path):
        """
        Der Pfad zur (Audio-)Datei.
        @type: String
        """
        self.__pathToFile = None
    
        """
        Mutagen-Audio-Handle, in welchem die Meta-Daten gespeichert werden.
        @type: unknown
        """
        self.__audioHandle = None    
    
        """
        Die benötigten Meta-Daten.
        @type: Dictionary
        """    
        self.__tagData = {
            'title'     : None,
            'artist'    : None,
            'trackOnCd' : None,
            'cd'        : None,
            'year'      : None,
            'length'    : None,
            'artistMbId': None,
            'cdMbId'    : None,
            'genres'    : None,
            'sort'      : None
        }
        if isinstance(path, str):
            self.__pathToFile = path
            self.__audioHandle = File(self.__pathToFile)
            self.__setTagData()    
        else: raise TagReaderException("Kein Pfad zur Audio-File angegeben")
    
    """
    ----------------------------------------------------------------------------
    Getter und Setter
    ----------------------------------------------------------------------------
    """
    
    """
    Getter.
    @return: Der Pfad zur (Audio-)Datei.
    @rtype: String
    """        
    def getpathToFile(self):
        return self.__pathToFile
    
    """
    Getter.
    @return: Die Meta-Daten der Datei.
    @rtype: Dictionary
    """
    def getTagData(self):
        return self.__tagData
    
    """
    ----------------------------------------------------------------------------
    Methoden der Klasse
    ----------------------------------------------------------------------------
    """
    
    """
    Hauptmethode. Liest die Meta-Daten der Datei ein und speichert sie im 
    Klassen-Dictionary. Wirft TagReaderExceptions!
    """
    def __setTagData(self):
        if self.__audioHandle is None:
            raise TagReaderException("Kein Audio-Handle erzeugt bei %s" 
                %(self.__pathToFile))
        else:
            try: 
                self.__tagData['title'] = str(self.__audioHandle['title'][0])
            except: pass
            try: 
                self.__tagData['artist'] = str(self.__audioHandle['artist'][0])
            except: pass
            try: 
                self.__tagData['cd'] = str(self.__audioHandle['album'][0])
            except: pass
            try: 
                self.__tagData['year'] = int(self.__audioHandle['date'][0][0:4])
            except: pass
            try: 
                self.__tagData['artistMbId'] = str(
                    self.__audioHandle['musicbrainz_artistid'][0])
            except: pass
            try: 
                self.__tagData['cdMbId'] = str(
                    self.__audioHandle['musicbrainz_albumid'][0])
            except: pass
            try: 
                self.__tagData['sort'] = str(
                    self.__audioHandle['artistsort'][0])
            except: pass
            try: 
                self.__tagData['genres'] = []
                for genre in self.__audioHandle['genre']:
                    self.__tagData['genres'].append(str(genre))
            except: self.__tagData['genres'] = None                
            self.__tagData['length'] = int(self.__audioHandle.info.length)
            if self.__audioHandle['tracknumber'][0].find('/') > -1:
                parts = self.__audioHandle['tracknumber'][0].split('/')
                self.__tagData['trackOnCd'] = int(parts[0])
            else: 
                self.__tagData['trackOnCd'] = int(
                    self.__audioHandle['tracknumber'][0])
